package org.destiny.cotroller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {

    /**
     * 初始化上传文件界面，跳转到index.jsp
     * 
     * @return
     */
    @RequestMapping(value = "/index", method = { RequestMethod.GET })
    public String index() {

        return "index";
    }

    /**
     * 上传文件方法
     * 
     * @param file
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequestMapping(value = "/upload1", method = { RequestMethod.POST })
    @ResponseBody
    public String upload1(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        // 上传目录地址
        String uploadDir = request.getSession().getServletContext().getRealPath("/") + "upload/";
        // 如果目录不存在，自动创建文件夹
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        // 上传文件名
        String fileName = file.getOriginalFilename();

        // 服务器端保存的文件对象
        File serverFile = new File(uploadDir + fileName);
        // 将上传的文件写入到服务器端文件内
        try {
            file.transferTo(serverFile);
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }

        return "上传成功";
    }

    /**
     * 自定义文件名
     * 
     * @param request
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload2", method = { RequestMethod.POST })
    @ResponseBody
    public String upload2(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        // 上传目录地址
        String uploadDir = request.getSession().getServletContext().getRealPath("/") + "upload/";
        // 如果目录不存在，自动创建文件夹
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        // 文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        // 上传文件名
        String fileName = UUID.randomUUID().toString() + suffix;

        // 服务器端保存的文件对象
        File serverFile = new File(uploadDir + fileName);
        // 将上传的文件写入到服务器端文件内
        try {
            file.transferTo(serverFile);
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }

        return "上传成功";
    }

    /**
     * 上传多个文件
     * 
     * @param request
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload3", method = { RequestMethod.POST })
    @ResponseBody
    public String upload3(HttpServletRequest request, @RequestParam("file") MultipartFile[] files) {
        // 上传目录地址
        String uploadDir = request.getSession().getServletContext().getRealPath("/") + "upload/";
        // 如果目录不存在，自动创建文件夹
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        try {
            for (MultipartFile file : files) {
                executeUpload(uploadDir, file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }

        return "上传成功";
    }

    /**
     * 提取上传方法为公共方法
     * 
     * @param uploadDir
     * @param file
     * @throws Exception
     */
    private void executeUpload(String uploadDir, MultipartFile file) throws Exception {
        // 文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        // 上传文件名
        String fileName = UUID.randomUUID().toString() + suffix;
        // 服务器端保存的文件对象
        File serverFile = new File(uploadDir + fileName);
        // 将上传文件写入到服务器文件内
        file.transferTo(serverFile);
    }

}
